<!DOCTYPE html />

<html>
<head>
	<title>ModbusMessageFactoryFixture.cs</title>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<link href="../../nocco.css" rel="stylesheet" media="all" type="text/css" />
	<script src="../../prettify.js" type="text/javascript"></script>
</head>
<body onload="prettyPrint()">
	<div id="container">
		<div id="background"></div>
			<div id="jump_to">
				Jump To &hellip;
				<div id="jump_wrapper">
					<div id="jump_page">
							<a class="source" href="../../nmodbus4/globalsuppressions.html">
								NModbus4\GlobalSuppressions.cs
							</a>
							<a class="source" href="../../nmodbus4/invalidmodbusrequestexception.html">
								NModbus4\InvalidModbusRequestException.cs
							</a>
							<a class="source" href="../../nmodbus4/modbus.html">
								NModbus4\Modbus.cs
							</a>
							<a class="source" href="../../nmodbus4/slaveexception.html">
								NModbus4\SlaveException.cs
							</a>
							<a class="source" href="../../nmodbus4/data/datastore.html">
								NModbus4\Data\DataStore.cs
							</a>
							<a class="source" href="../../nmodbus4/data/datastoreeventargs.html">
								NModbus4\Data\DataStoreEventArgs.cs
							</a>
							<a class="source" href="../../nmodbus4/data/datastorefactory.html">
								NModbus4\Data\DataStoreFactory.cs
							</a>
							<a class="source" href="../../nmodbus4/data/discretecollection.html">
								NModbus4\Data\DiscreteCollection.cs
							</a>
							<a class="source" href="../../nmodbus4/data/imodbusmessagedatacollection.html">
								NModbus4\Data\IModbusMessageDataCollection.cs
							</a>
							<a class="source" href="../../nmodbus4/data/modbusdatacollection.html">
								NModbus4\Data\ModbusDataCollection.cs
							</a>
							<a class="source" href="../../nmodbus4/data/modbusdatatype.html">
								NModbus4\Data\ModbusDataType.cs
							</a>
							<a class="source" href="../../nmodbus4/data/registercollection.html">
								NModbus4\Data\RegisterCollection.cs
							</a>
							<a class="source" href="../../nmodbus4/device/imodbusmaster.html">
								NModbus4\Device\IModbusMaster.cs
							</a>
							<a class="source" href="../../nmodbus4/device/imodbusserialmaster.html">
								NModbus4\Device\IModbusSerialMaster.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusdevice.html">
								NModbus4\Device\ModbusDevice.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusipmaster.html">
								NModbus4\Device\ModbusIpMaster.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusmaster.html">
								NModbus4\Device\ModbusMaster.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusmastertcpconnection.html">
								NModbus4\Device\ModbusMasterTcpConnection.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusserialmaster.html">
								NModbus4\Device\ModbusSerialMaster.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusserialslave.html">
								NModbus4\Device\ModbusSerialSlave.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusslave.html">
								NModbus4\Device\ModbusSlave.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusslaverequesteventargs.html">
								NModbus4\Device\ModbusSlaveRequestEventArgs.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbustcpslave.html">
								NModbus4\Device\ModbusTcpSlave.cs
							</a>
							<a class="source" href="../../nmodbus4/device/modbusudpslave.html">
								NModbus4\Device\ModbusUdpSlave.cs
							</a>
							<a class="source" href="../../nmodbus4/device/tcpconnectioneventargs.html">
								NModbus4\Device\TcpConnectionEventArgs.cs
							</a>
							<a class="source" href="../../nmodbus4/extensions/enron/enronmodbus.html">
								NModbus4\Extensions\Enron\EnronModbus.cs
							</a>
							<a class="source" href="../../nmodbus4/io/emptytransport.html">
								NModbus4\IO\EmptyTransport.cs
							</a>
							<a class="source" href="../../nmodbus4/io/istreamresource.html">
								NModbus4\IO\IStreamResource.cs
							</a>
							<a class="source" href="../../nmodbus4/io/modbusasciitransport.html">
								NModbus4\IO\ModbusAsciiTransport.cs
							</a>
							<a class="source" href="../../nmodbus4/io/modbusiptransport.html">
								NModbus4\IO\ModbusIpTransport.cs
							</a>
							<a class="source" href="../../nmodbus4/io/modbusrtutransport.html">
								NModbus4\IO\ModbusRtuTransport.cs
							</a>
							<a class="source" href="../../nmodbus4/io/modbusserialtransport.html">
								NModbus4\IO\ModbusSerialTransport.cs
							</a>
							<a class="source" href="../../nmodbus4/io/modbustransport.html">
								NModbus4\IO\ModbusTransport.cs
							</a>
							<a class="source" href="../../nmodbus4/io/serialportadapter.html">
								NModbus4\IO\SerialPortAdapter.cs
							</a>
							<a class="source" href="../../nmodbus4/io/streamresourceutility.html">
								NModbus4\IO\StreamResourceUtility.cs
							</a>
							<a class="source" href="../../nmodbus4/io/tcpclientadapter.html">
								NModbus4\IO\TcpClientAdapter.cs
							</a>
							<a class="source" href="../../nmodbus4/io/udpclientadapter.html">
								NModbus4\IO\UdpClientAdapter.cs
							</a>
							<a class="source" href="../../nmodbus4/message/abstractmodbusmessage.html">
								NModbus4\Message\AbstractModbusMessage.cs
							</a>
							<a class="source" href="../../nmodbus4/message/abstractmodbusmessagewithdata.html">
								NModbus4\Message\AbstractModbusMessageWithData.cs
							</a>
							<a class="source" href="../../nmodbus4/message/diagnosticsrequestresponse.html">
								NModbus4\Message\DiagnosticsRequestResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/message/imodbusmessage.html">
								NModbus4\Message\IModbusMessage.cs
							</a>
							<a class="source" href="../../nmodbus4/message/imodbusrequest.html">
								NModbus4\Message\IModbusRequest.cs
							</a>
							<a class="source" href="../../nmodbus4/message/modbusmessagefactory.html">
								NModbus4\Message\ModbusMessageFactory.cs
							</a>
							<a class="source" href="../../nmodbus4/message/modbusmessageimpl.html">
								NModbus4\Message\ModbusMessageImpl.cs
							</a>
							<a class="source" href="../../nmodbus4/message/readcoilsinputsrequest.html">
								NModbus4\Message\ReadCoilsInputsRequest.cs
							</a>
							<a class="source" href="../../nmodbus4/message/readcoilsinputsresponse.html">
								NModbus4\Message\ReadCoilsInputsResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/message/readholdinginputregistersrequest.html">
								NModbus4\Message\ReadHoldingInputRegistersRequest.cs
							</a>
							<a class="source" href="../../nmodbus4/message/readholdinginputregistersresponse.html">
								NModbus4\Message\ReadHoldingInputRegistersResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/message/readwritemultipleregistersrequest.html">
								NModbus4\Message\ReadWriteMultipleRegistersRequest.cs
							</a>
							<a class="source" href="../../nmodbus4/message/slaveexceptionresponse.html">
								NModbus4\Message\SlaveExceptionResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/message/writemultiplecoilsrequest.html">
								NModbus4\Message\WriteMultipleCoilsRequest.cs
							</a>
							<a class="source" href="../../nmodbus4/message/writemultiplecoilsresponse.html">
								NModbus4\Message\WriteMultipleCoilsResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/message/writemultipleregistersrequest.html">
								NModbus4\Message\WriteMultipleRegistersRequest.cs
							</a>
							<a class="source" href="../../nmodbus4/message/writemultipleregistersresponse.html">
								NModbus4\Message\WriteMultipleRegistersResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/message/writesinglecoilrequestresponse.html">
								NModbus4\Message\WriteSingleCoilRequestResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/message/writesingleregisterrequestresponse.html">
								NModbus4\Message\WriteSingleRegisterRequestResponse.cs
							</a>
							<a class="source" href="../../nmodbus4/properties/assemblyinfo.html">
								NModbus4\Properties\AssemblyInfo.cs
							</a>
							<a class="source" href="../../nmodbus4/unme.common/disposableutility.html">
								NModbus4\Unme.Common\DisposableUtility.cs
							</a>
							<a class="source" href="../../nmodbus4/unme.common/eventutility.html">
								NModbus4\Unme.Common\EventUtility.cs
							</a>
							<a class="source" href="../../nmodbus4/unme.common/sequenceutility.html">
								NModbus4\Unme.Common\SequenceUtility.cs
							</a>
							<a class="source" href="../../nmodbus4/utility/discriminatedunion.html">
								NModbus4\Utility\DiscriminatedUnion.cs
							</a>
							<a class="source" href="../../nmodbus4/utility/modbusutility.html">
								NModbus4\Utility\ModbusUtility.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/slaveexceptionfixture.html">
								NModbus4.UnitTests\SlaveExceptionFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/data/datastoreeventargsfixture.html">
								NModbus4.UnitTests\Data\DataStoreEventArgsFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/data/datastorefixture.html">
								NModbus4.UnitTests\Data\DataStoreFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/data/discretecollectionfixture.html">
								NModbus4.UnitTests\Data\DiscreteCollectionFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/data/modbusdatacollectionfixture.html">
								NModbus4.UnitTests\Data\ModbusDataCollectionFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/data/registercollectionfixture.html">
								NModbus4.UnitTests\Data\RegisterCollectionFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/device/modbusmasterfixture.html">
								NModbus4.UnitTests\Device\ModbusMasterFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/device/modbusslavefixture.html">
								NModbus4.UnitTests\Device\ModbusSlaveFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/device/tcpconnectioneventargsfixture.html">
								NModbus4.UnitTests\Device\TcpConnectionEventArgsFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/io/modbusasciitransportfixture.html">
								NModbus4.UnitTests\IO\ModbusAsciiTransportFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/io/modbusrtutransportfixture.html">
								NModbus4.UnitTests\IO\ModbusRtuTransportFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/io/modbusserialtransportfixture.html">
								NModbus4.UnitTests\IO\ModbusSerialTransportFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/io/modbustcptransportfixture.html">
								NModbus4.UnitTests\IO\ModbusTcpTransportFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/io/modbustransportfixture.html">
								NModbus4.UnitTests\IO\ModbusTransportFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/io/udpclientadapterfixture.html">
								NModbus4.UnitTests\IO\UdpClientAdapterFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/diagnosticsrequestresponsefixture.html">
								NModbus4.UnitTests\Message\DiagnosticsRequestResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/messageutility.html">
								NModbus4.UnitTests\Message\MessageUtility.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/modbusmessagefactoryfixture.html">
								NModbus4.UnitTests\Message\ModbusMessageFactoryFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/modbusmessagefixture.html">
								NModbus4.UnitTests\Message\ModbusMessageFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/modbusmessageimplfixture.html">
								NModbus4.UnitTests\Message\ModbusMessageImplFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/modbusmessagewithdatafixture.html">
								NModbus4.UnitTests\Message\ModbusMessageWithDataFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/readcoilsinputsrequestfixture.html">
								NModbus4.UnitTests\Message\ReadCoilsInputsRequestFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/readcoilsinputsresponsefixture.html">
								NModbus4.UnitTests\Message\ReadCoilsInputsResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/readholdinginputregistersrequestfixture.html">
								NModbus4.UnitTests\Message\ReadHoldingInputRegistersRequestFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/readholdinginputregistersresponsefixture.html">
								NModbus4.UnitTests\Message\ReadHoldingInputRegistersResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/readwritemultipleregistersrequestfixture.html">
								NModbus4.UnitTests\Message\ReadWriteMultipleRegistersRequestFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/returnquerydatarequestresponsefixture.html">
								NModbus4.UnitTests\Message\ReturnQueryDataRequestResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/slaveexceptionresponsefixture.html">
								NModbus4.UnitTests\Message\SlaveExceptionResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/writemultiplecoilsrequestfixture.html">
								NModbus4.UnitTests\Message\WriteMultipleCoilsRequestFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/writemultiplecoilsresponsefixture.html">
								NModbus4.UnitTests\Message\WriteMultipleCoilsResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/writemultipleregistersrequestfixture.html">
								NModbus4.UnitTests\Message\WriteMultipleRegistersRequestFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/writemultipleregistersresponsefixture.html">
								NModbus4.UnitTests\Message\WriteMultipleRegistersResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/writesinglecoilrequestresponsefixture.html">
								NModbus4.UnitTests\Message\WriteSingleCoilRequestResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/message/writesingleregisterrequestresponsefixture.html">
								NModbus4.UnitTests\Message\WriteSingleRegisterRequestResponseFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/properties/assemblyinfo.html">
								NModbus4.UnitTests\Properties\AssemblyInfo.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/utility/collectionutilityfixture.html">
								NModbus4.UnitTests\Utility\CollectionUtilityFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/utility/discriminatedunionfixture.html">
								NModbus4.UnitTests\Utility\DiscriminatedUnionFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/utility/modbusutilityfixture.html">
								NModbus4.UnitTests\Utility\ModbusUtilityFixture.cs
							</a>
							<a class="source" href="../../nmodbus4.unittests/utility/serialconnectionfixture.html">
								NModbus4.UnitTests\Utility\SerialConnectionFixture.cs
							</a>
							<a class="source" href="../../samples/driver.html">
								Samples\Driver.cs
							</a>
							<a class="source" href="../../samples/properties/assemblyinfo.html">
								Samples\Properties\AssemblyInfo.cs
							</a>
					</div>
				</div>
			</div>
		<table cellpadding="0" cellspacing="0">
			<thead>
				<tr>
					<th class="docs">
						<h1>ModbusMessageFactoryFixture.cs</h1>
					</th>
					<th class="code"></th>
				</tr>
			</thead>
			<tbody>
					<tr id="section_1">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_1">&#182;</a>
							</div>
							
						</td>
						<td class="code">
							<pre><code class='prettyprint'>using System;
using System.Linq;
using Modbus.Data;
using Modbus.Message;

namespace Modbus.UnitTests.Message
{
    using Unme.Common;
    using NUnit.Framework;

    [TestFixture]
    public class ModbusMessageFactoryFixture : ModbusMessageFixture
    {
        [Test]
        public void CreateModbusMessageReadCoilsRequest()
        {
            ReadCoilsInputsRequest request =
                ModbusMessageFactory.CreateModbusMessage&lt;ReadCoilsInputsRequest&gt;(new byte[]
                {11, Modbus.ReadCoils, 0, 19, 0, 37});
            ReadCoilsInputsRequest expectedRequest = new ReadCoilsInputsRequest(Modbus.ReadCoils, 11, 19, 37);
            AssertModbusMessagePropertiesAreEqual(request, expectedRequest);
            Assert.AreEqual(expectedRequest.StartAddress, request.StartAddress);
            Assert.AreEqual(expectedRequest.NumberOfPoints, request.NumberOfPoints);
        }

        [Test]
        [ExpectedException(typeof (FormatException))]
        public void CreateModbusMessageReadCoilsRequestWithInvalidFrameSize()
        {
            byte[] frame = {11, Modbus.ReadCoils, 4, 1, 2};
            ReadCoilsInputsRequest request = ModbusMessageFactory.CreateModbusMessage&lt;ReadCoilsInputsRequest&gt;(frame);
            Assert.Fail();
        }

        [Test]
        public void CreateModbusMessageReadCoilsResponse()
        {
            ReadCoilsInputsResponse response =
                ModbusMessageFactory.CreateModbusMessage&lt;ReadCoilsInputsResponse&gt;(new byte[]
                {11, Modbus.ReadCoils, 1, 1});
            ReadCoilsInputsResponse expectedResponse = new ReadCoilsInputsResponse(Modbus.ReadCoils, 11, 1,
                new DiscreteCollection(true, false, false, false));
            AssertModbusMessagePropertiesAreEqual(expectedResponse, response);
            Assert.AreEqual(expectedResponse.Data.NetworkBytes, response.Data.NetworkBytes);
        }

        [Test]
        [ExpectedException(typeof (FormatException))]
        public void CreateModbusMessageReadCoilsResponseWithNoByteCount()
        {
            byte[] frame = {11, Modbus.ReadCoils};
            ReadCoilsInputsResponse response = ModbusMessageFactory.CreateModbusMessage&lt;ReadCoilsInputsResponse&gt;(frame);
            Assert.Fail();
        }

        [Test]
        [ExpectedException(typeof (FormatException))]
        public void CreateModbusMessageReadCoilsResponseWithInvalidDataSize()
        {
            byte[] frame = {11, Modbus.ReadCoils, 4, 1, 2, 3};
            ReadCoilsInputsResponse response = ModbusMessageFactory.CreateModbusMessage&lt;ReadCoilsInputsResponse&gt;(frame);
            Assert.Fail();
        }

        [Test]
        public void CreateModbusMessageReadHoldingRegistersRequest()
        {
            ReadHoldingInputRegistersRequest request =
                ModbusMessageFactory.CreateModbusMessage&lt;ReadHoldingInputRegistersRequest&gt;(new byte[]
                {17, Modbus.ReadHoldingRegisters, 0, 107, 0, 3});
            ReadHoldingInputRegistersRequest expectedRequest =
                new ReadHoldingInputRegistersRequest(Modbus.ReadHoldingRegisters, 17, 107, 3);
            AssertModbusMessagePropertiesAreEqual(expectedRequest, request);
            Assert.AreEqual(expectedRequest.StartAddress, request.StartAddress);
            Assert.AreEqual(expectedRequest.NumberOfPoints, request.NumberOfPoints);
        }

        [Test]
        [ExpectedException(typeof (FormatException))]
        public void CreateModbusMessageReadHoldingRegistersRequestWithInvalidFrameSize()
        {
            ReadHoldingInputRegistersRequest request =
                ModbusMessageFactory.CreateModbusMessage&lt;ReadHoldingInputRegistersRequest&gt;(new byte[]
                {11, Modbus.ReadHoldingRegisters, 0, 0, 5});
        }

        [Test]
        public void CreateModbusMessageReadHoldingRegistersResponse()
        {
            ReadHoldingInputRegistersResponse response =
                ModbusMessageFactory.CreateModbusMessage&lt;ReadHoldingInputRegistersResponse&gt;(new byte[]
                {11, Modbus.ReadHoldingRegisters, 4, 0, 3, 0, 4});
            ReadHoldingInputRegistersResponse expectedResponse =
                new ReadHoldingInputRegistersResponse(Modbus.ReadHoldingRegisters, 11, new RegisterCollection(3, 4));
            AssertModbusMessagePropertiesAreEqual(expectedResponse, response);
        }

        [Test]
        [ExpectedException(typeof (FormatException))]
        public void CreateModbusMessageReadHoldingRegistersResponseWithInvalidFrameSize()
        {
            ModbusMessageFactory.CreateModbusMessage&lt;ReadHoldingInputRegistersResponse&gt;(new byte[]
            {11, Modbus.ReadHoldingRegisters});
        }

        [Test]
        public void CreateModbusMessageSlaveExceptionResponse()
        {
            SlaveExceptionResponse response =
                ModbusMessageFactory.CreateModbusMessage&lt;SlaveExceptionResponse&gt;(new byte[] {11, 129, 2});
            SlaveExceptionResponse expectedException = new SlaveExceptionResponse(11,
                Modbus.ReadCoils + Modbus.ExceptionOffset, 2);
            Assert.AreEqual(expectedException.FunctionCode, response.FunctionCode);
            Assert.AreEqual(expectedException.SlaveAddress, response.SlaveAddress);
            Assert.AreEqual(expectedException.MessageFrame, response.MessageFrame);
            Assert.AreEqual(expectedException.ProtocolDataUnit, response.ProtocolDataUnit);
        }

        [Test]
        [ExpectedException(typeof (FormatException))]
        public void CreateModbusMessageSlaveExceptionResponseWithInvalidFunctionCode()
        {
            SlaveExceptionResponse response =
                ModbusMessageFactory.CreateModbusMessage&lt;SlaveExceptionResponse&gt;(new byte[] {11, 128, 2});
        }

        [Test]
        [ExpectedException(typeof (FormatException))]
        public void CreateModbusMessageSlaveExceptionResponseWithInvalidFrameSize()
        {
            ModbusMessageFactory.CreateModbusMessage&lt;SlaveExceptionResponse&gt;(new byte[] {11, 128});
            Assert.Fail();
        }

        [Test]
        public void CreateModbusMessageWriteSingleCoilRequestResponse()
        {
            WriteSingleCoilRequestResponse request =
                ModbusMessageFactory.CreateModbusMessage&lt;WriteSingleCoilRequestResponse&gt;(new byte[]
                {17, Modbus.WriteSingleCoil, 0, 172, byte.MaxValue, 0});
            WriteSingleCoilRequestResponse expectedRequest = new WriteSingleCoilRequestResponse(17, 172, true);
            AssertModbusMessagePropertiesAreEqual(expectedRequest, request);
            Assert.AreEqual(expectedRequest.StartAddress, request.StartAddress);
            Assert.AreEqual(expectedRequest.Data.NetworkBytes, request.Data.NetworkBytes);
        }

        [Test]
        [ExpectedException(typeof (FormatException))]
        public void CreateModbusMessageWriteSingleCoilRequestResponseWithInvalidFrameSize()
        {
            WriteSingleCoilRequestResponse request =
                ModbusMessageFactory.CreateModbusMessage&lt;WriteSingleCoilRequestResponse&gt;(new byte[]
                {11, Modbus.WriteSingleCoil, 0, 105, byte.MaxValue});
            Assert.Fail();
        }

        [Test]
        public void CreateModbusMessageWriteSingleRegisterRequestResponse()
        {
            WriteSingleRegisterRequestResponse request =
                ModbusMessageFactory.CreateModbusMessage&lt;WriteSingleRegisterRequestResponse&gt;(new byte[]
                {17, Modbus.WriteSingleRegister, 0, 1, 0, 3});
            WriteSingleRegisterRequestResponse expectedRequest = new WriteSingleRegisterRequestResponse(17, 1, 3);
            AssertModbusMessagePropertiesAreEqual(expectedRequest, request);
            Assert.AreEqual(expectedRequest.StartAddress, request.StartAddress);
            Assert.AreEqual(expectedRequest.Data.NetworkBytes, request.Data.NetworkBytes);
        }

        [Test]
        [ExpectedException(typeof (FormatException))]
        public void CreateModbusMessageWriteSingleRegisterRequestResponseWithInvalidFrameSize()
        {
            WriteSingleRegisterRequestResponse request =
                ModbusMessageFactory.CreateModbusMessage&lt;WriteSingleRegisterRequestResponse&gt;(new byte[]
                {11, Modbus.WriteSingleRegister, 0, 1, 0});
            Assert.Fail();
        }

        [Test]
        public void CreateModbusMessageWriteMultipleRegistersRequest()
        {
            WriteMultipleRegistersRequest request =
                ModbusMessageFactory.CreateModbusMessage&lt;WriteMultipleRegistersRequest&gt;(new byte[]
                {11, Modbus.WriteMultipleRegisters, 0, 5, 0, 1, 2, 255, 255});
            WriteMultipleRegistersRequest expectedRequest = new WriteMultipleRegistersRequest(11, 5,
                new RegisterCollection(ushort.MaxValue));
            AssertModbusMessagePropertiesAreEqual(expectedRequest, request);
            Assert.AreEqual(expectedRequest.StartAddress, request.StartAddress);
            Assert.AreEqual(expectedRequest.NumberOfPoints, request.NumberOfPoints);
            Assert.AreEqual(expectedRequest.ByteCount, request.ByteCount);
            Assert.AreEqual(expectedRequest.Data.NetworkBytes, request.Data.NetworkBytes);
        }

        [Test]
        [ExpectedException(typeof (FormatException))]
        public void CreateModbusMessageWriteMultipleRegistersRequestWithInvalidFrameSize()
        {
            WriteMultipleRegistersRequest request =
                ModbusMessageFactory.CreateModbusMessage&lt;WriteMultipleRegistersRequest&gt;(new byte[]
                {11, Modbus.WriteMultipleRegisters, 0, 5, 0, 1, 2});
            Assert.Fail();
        }

        [Test]
        public void CreateModbusMessageWriteMultipleRegistersResponse()
        {
            WriteMultipleRegistersResponse response =
                ModbusMessageFactory.CreateModbusMessage&lt;WriteMultipleRegistersResponse&gt;(new byte[]
                {17, Modbus.WriteMultipleRegisters, 0, 1, 0, 2});
            WriteMultipleRegistersResponse expectedResponse = new WriteMultipleRegistersResponse(17, 1, 2);
            AssertModbusMessagePropertiesAreEqual(expectedResponse, response);
            Assert.AreEqual(expectedResponse.StartAddress, response.StartAddress);
            Assert.AreEqual(expectedResponse.NumberOfPoints, response.NumberOfPoints);
        }

        [Test]
        public void CreateModbusMessageWriteMultipleCoilsRequest()
        {
            WriteMultipleCoilsRequest request =
                ModbusMessageFactory.CreateModbusMessage&lt;WriteMultipleCoilsRequest&gt;(new byte[]
                {17, Modbus.WriteMultipleCoils, 0, 19, 0, 10, 2, 205, 1});
            WriteMultipleCoilsRequest expectedRequest = new WriteMultipleCoilsRequest(17, 19,
                new DiscreteCollection(true, false, true, true, false, false, true, true, true, false));
            AssertModbusMessagePropertiesAreEqual(expectedRequest, request);
            Assert.AreEqual(expectedRequest.StartAddress, request.StartAddress);
            Assert.AreEqual(expectedRequest.NumberOfPoints, request.NumberOfPoints);
            Assert.AreEqual(expectedRequest.ByteCount, request.ByteCount);
            Assert.AreEqual(expectedRequest.Data.NetworkBytes, request.Data.NetworkBytes);
        }

        [Test]
        [ExpectedException(typeof (FormatException))]
        public void CreateModbusMessageWriteMultipleCoilsRequestWithInvalidFrameSize()
        {
            WriteMultipleCoilsRequest request =
                ModbusMessageFactory.CreateModbusMessage&lt;WriteMultipleCoilsRequest&gt;(new byte[]
                {17, Modbus.WriteMultipleCoils, 0, 19, 0, 10, 2});
            Assert.Fail();
        }

        [Test]
        public void CreateModbusMessageWriteMultipleCoilsResponse()
        {
            WriteMultipleCoilsResponse response =
                ModbusMessageFactory.CreateModbusMessage&lt;WriteMultipleCoilsResponse&gt;(new byte[]
                {17, Modbus.WriteMultipleCoils, 0, 19, 0, 10});
            WriteMultipleCoilsResponse expectedResponse = new WriteMultipleCoilsResponse(17, 19, 10);
            AssertModbusMessagePropertiesAreEqual(expectedResponse, response);
            Assert.AreEqual(expectedResponse.StartAddress, response.StartAddress);
            Assert.AreEqual(expectedResponse.NumberOfPoints, response.NumberOfPoints);
        }

        [Test]
        [ExpectedException(typeof (FormatException))]
        public void CreateModbusMessageWriteMultipleCoilsResponseWithInvalidFrameSize()
        {
            WriteMultipleCoilsResponse request =
                ModbusMessageFactory.CreateModbusMessage&lt;WriteMultipleCoilsResponse&gt;(new byte[]
                {17, Modbus.WriteMultipleCoils, 0, 19, 0});
            Assert.Fail();
        }

        [Test]
        public void CreateModbusMessageReadWriteMultipleRegistersRequest()
        {
            ReadWriteMultipleRegistersRequest request =
                ModbusMessageFactory.CreateModbusMessage&lt;ReadWriteMultipleRegistersRequest&gt;(new byte[]
                {0x05, 0x17, 0x00, 0x03, 0x00, 0x06, 0x00, 0x0e, 0x00, 0x03, 0x06, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff});
            RegisterCollection writeCollection = new RegisterCollection(255, 255, 255);
            ReadWriteMultipleRegistersRequest expectedRequest = new ReadWriteMultipleRegistersRequest(5, 3, 6, 14,
                writeCollection);
            AssertModbusMessagePropertiesAreEqual(expectedRequest, request);
        }

        [Test]
        [ExpectedException(typeof (FormatException))]
        public void CreateModbusMessageReadWriteMultipleRegistersRequestWithInvalidFrameSize()
        {
            byte[] frame = {17, Modbus.ReadWriteMultipleRegisters, 1, 2, 3};
            ReadWriteMultipleRegistersRequest request =
                ModbusMessageFactory.CreateModbusMessage&lt;ReadWriteMultipleRegistersRequest&gt;(frame);
            Assert.Fail();
        }

        [Test]
        public void CreateModbusMessageReturnQueryDataRequestResponse()
        {
            const byte slaveAddress = 5;
            RegisterCollection data = new RegisterCollection(50);
            byte[] frame = new byte[] { slaveAddress, 8, 0, 0 }.Concat(data.NetworkBytes).ToArray();
            DiagnosticsRequestResponse message =
                ModbusMessageFactory.CreateModbusMessage&lt;DiagnosticsRequestResponse&gt;(frame);
            DiagnosticsRequestResponse expectedMessage =
                new DiagnosticsRequestResponse(Modbus.DiagnosticsReturnQueryData, slaveAddress, data);

            Assert.AreEqual(expectedMessage.SubFunctionCode, message.SubFunctionCode);
            AssertModbusMessagePropertiesAreEqual(expectedMessage, message);
        }

        [Test]
        [ExpectedException(typeof (FormatException))]
        public void CreateModbusMessageReturnQueryDataRequestResponseTooSmall()
        {
            byte[] frame = new byte[] {5, 8, 0, 0, 5};
            DiagnosticsRequestResponse message =
                ModbusMessageFactory.CreateModbusMessage&lt;DiagnosticsRequestResponse&gt;(frame);
        }

        [Test, ExpectedException(typeof (FormatException))]
        public void CreateModbusRequestWithInvalidMessageFrame()
        {
            ModbusMessageFactory.CreateModbusRequest(new byte[] {0, 1});
            Assert.Fail();
        }

        [Test, ExpectedException(typeof (ArgumentException))]
        public void CreateModbusRequestWithInvalidFunctionCode()
        {
            ModbusMessageFactory.CreateModbusRequest(new byte[] {1, 99, 0, 0, 0, 1, 23});
            Assert.Fail();
        }

        [Test]
        public void CreateModbusRequestForReadCoils()
        {
            ReadCoilsInputsRequest req = new ReadCoilsInputsRequest(1, 2, 1, 10);
            IModbusMessage request = ModbusMessageFactory.CreateModbusRequest(req.MessageFrame);
            Assert.AreEqual(typeof (ReadCoilsInputsRequest), request.GetType());
        }

        [Test]
        public void CreateModbusRequestForDiagnostics()
        {
            DiagnosticsRequestResponse diagnosticsRequest = new DiagnosticsRequestResponse(0, 2,
                new RegisterCollection(45));
            IModbusMessage request = ModbusMessageFactory.CreateModbusRequest(diagnosticsRequest.MessageFrame);
            Assert.AreEqual(typeof (DiagnosticsRequestResponse), request.GetType());
        }
    }
}
</code></pre>
						</td>
					</tr>
			</tbody>
		</table>
	</div>
</body>
</html>
